package com.zhuoyue.web.utils;

import com.zhuoyue.common.core.domain.model.LoginUser;
import com.zhuoyue.framework.web.service.TokenService;
import com.zhuoyue.system.mapper.SysDeptMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @author hewenze
 */
@Service
public class OrgCodeUtils {

    @Autowired
    private TokenService tokenService;


    @Autowired
    private SysDeptMapper sysDeptMapper;


    public String getOrgCode(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        String sysDeptOrgCode = this.sysDeptMapper.selectDeptById(loginUser.getUser().getDept().getDeptId()).getOrgCode();
        if (!StringUtils.isEmpty(sysDeptOrgCode)) {

            return sysDeptOrgCode;
        } else {
            return null;
        }

    }

    public String getUserToken(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        String token = loginUser.getToken();
        if (!StringUtils.isEmpty(token)) {

            return token;
        } else {
            return null;
        }

    }

    public String getUserName(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        String userName = loginUser.getUsername();
        if (!StringUtils.isEmpty(userName)) {

            return userName;
        } else {
            return null;
        }

    }

    public LoginUser getLoginUser(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        //String userName = loginUser.getUsername();
        if (loginUser != null) {

            return loginUser;
        } else {
            return null;
        }

    }


}
